package service.impl;

import dao.UserDao;
import dao.impl.UserDaoImpl;
import model.Query;
import model.bo.AdminUserBO;
import model.bo.UserSearchBO;
import model.InnerResult;
import model.po.User;
import service.UserService;
import utils.ModelUtils;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();

    /**
     * 获取系统中全部的用户资料
     * @return
     */
    @Override
    public InnerResult getAllUsers() {
        List<User> userList = userDao.getAllUsers();
        if (null == userList) { return InnerResult.fulfill(); }

        List<AdminUserBO> users = new ArrayList<>(userList.size());
        for (User u : userList) {
            AdminUserBO user = new AdminUserBO();
            ModelUtils.copyProperties(user, u);
            users.add(user);
        }
        return InnerResult.fulfill(users);
    }

    /**
     * 删除用户
     * @param query
     * @return
     */
    @Override
    public InnerResult deleteUser(Query query) {
        User user = new User();
        ModelUtils.queryToModel(query, user);
        int result = userDao.deleteUser(user);
        return result > 0 ? InnerResult.fulfill() : InnerResult.reject("删除失败");
    }

    /**
     * 通过nickname查询用户
     * @param query
     * @return
     */
    @Override
    public InnerResult searchUser(Query query) {
        UserSearchBO nickname = new UserSearchBO();
        ModelUtils.queryToModel(query, nickname);

        User user = new User();
        user.setNickname(nickname.getWord());
        List<User> userList = userDao.getSearchUser(user);
        List<AdminUserBO> users = new ArrayList<>(userList.size());
        for (User u : userList) {
            AdminUserBO tmp = new AdminUserBO();
            ModelUtils.copyProperties(tmp, u);
            users.add(tmp);
        }
        return InnerResult.fulfill(users);
    }
}
